package jnBase
{
	import jnBase.components.Dlg_XunWen;
	import jnBase.eventdispatcher.JNEventDispatcher;
	import jnBase.view.PaiHangView;
	import jnBase.view.SceneToolControl;
	import jnBase.view.SceneToolsWAP;
	
	import laya.events.Event;
	import laya.net.HttpRequest;
	import laya.net.LocalStorage;
	import laya.utils.Browser;
	import laya.utils.Handler;
	
	import ui.shiyan.SYBaoGaoUI;
	
	public class PBaoGao extends SYBaoGaoUI
	{
		/**
		 *服务器上的实验报告数据 包括实验结果
		 */		
		protected var _baoGaoData:Object={};
		private  var _createUserName:String="";//实验人员
		private  var _myTeacher:String="";//指导老师
		private  var _shiyanName:String="";//实验名称
		private  var _shiyanMudi:String="";//实验目的
		private  var _shiyanYuanLi:String="";//实验原理
		private  var _shiyanDuixiang:String="";//实验对象
		private  var _shiyanQiCai:String="";//实验器材
		private  var _shiyanYaoPin:String="";//实验药品
		private  var _shiyanBuzhou:String="";//实验步骤
		protected  var _syJieGuo:Object={};//实验结果
		private  var _shiyanTaoLun:String="";//实验讨论
		private  var _shiyanJieLun:String="";//实验结论
		protected var getSuccess:Boolean=false;
		
		private var http:HttpRequest;
		public function PBaoGao()
		{
			super();
			this.dragArea="0,0,1067,80";
		}
		
		override public function onAwake():void
		{
			// TODO Auto Generated method stub
			btn_submit.on(Event.CLICK,this,submitHandler);
			JNEventDispatcher.getInstance().on("TimeOut",this,timeOut);
			super.onAwake();
			trace("实验报告 awake");
			getShiyanData();
			if(Data.timeLimit && Data.isExam && Data.UsedTime>=Data.examTime){
				timeOut();
			}
		}
		
		/**
		 *实验报告窗口打开时每1s上传一次实验报告数据 
		 * 
		 */
		override public function onOpened(param:*):void
		{
			// TODO Auto Generated method stub
			super.onOpened(param);
			trace("实验报告 opened");
			if(this.active){
				//更新实验结果表格里面的数据
				updateJieGuo();
				Laya.timer.loop(5000,this,updateJieGuo);
			}
		}
		protected function updateJieGuo():void
		{
			// TODO Auto Generated method stub
			trace("实验报告:update");
			if(getSuccess){
				if(ShiYanJieGuo.getInstance().active) ShiYanJieGuo.getInstance().update();
				saveToLocal();
			}
		}
		/**
		 * 缓存到浏览器
		 */
		public function saveToLocal():void
		{
			// TODO Auto Generated method stub
			var data:Array=[];
			data.push({itemname:'指导老师',itemvalue:myTeacher,itemsort:0})
			data.push({itemname:'实验人员',itemvalue:createUserName,itemsort:1})
			data.push({itemname:'实验名称',itemvalue:syName,itemsort:2})
			data.push({itemname:'实验目的',itemvalue:syMudi,itemsort:3})
			data.push({itemname:'实验原理',itemvalue:syYuanLi,itemsort:4})
			data.push({itemname:'实验对象',itemvalue:syDuixiang,itemsort:5})
			data.push({itemname:'实验器材',itemvalue:syQiCai,itemsort:6})
			data.push({itemname:'药品试剂',itemvalue:syYaoPin,itemsort:7})
			data.push({itemname:'实验步骤',itemvalue:syBuzhou,itemsort:8})
			data.push({itemname:'实验结果',itemvalue:JSON.stringify(syJieGuo),itemsort:9})
			data.push({itemname:'实验讨论',itemvalue:syTaoLun,itemsort:10})
			data.push({itemname:'实验结论',itemvalue:syJieLun,itemsort:11})
			LocalStorage.setJSON('report'+Data.PracticeId,data);
		}
		/**
		 *实验报告关闭时再同步一次实验结果并上传实验报告数据 
		 * @param type
		 * 
		 */		
		override public function onClosed(type:String=null):void
		{
			// TODO Auto Generated method stub
			super.onClosed(type);
			ShiYanBaoGao.getInstance().event("close");
			Laya.timer.clear(this,updateJieGuo);
			updateJieGuo();
		}
		/**
		 *将实验结果中的数据更新到实验报告中. 
		 * 
		 */		
		public function update():void{}
		/**
		 *获取实验数据  实验报告 
		 * 
		 */		
		public function getShiyanData():void{
			trace("获取实验报告数据"+'report'+Data.PracticeId);
			
			if(Data.token){
				//为了兼容之前的实验数据，这里仍然从服务器获取缓存数据,但是不再上传到服务器，而是保存到浏览器缓存。
				Api.GetReportByStuCheckRecordId(Data.operationType,Data.PracticeId,this,getServerData)
			}else{
				LocalStorage.setJSON('report'+Data.PracticeId,{value:null});
				ShiYanBaoGao.getInstance().event("getData");
				getSuccess=true;
			}
			
			function getServerData(da:Object):void{
				trace("reportreportreportreport:",da.rows.checkreportvalues);
				var data:Object=da.rows.checkreportvalues?da.rows.checkreportvalues:[]
				setData(data);
				LocalStorage.setJSON('report'+Data.PracticeId,data);
			}
			function setData(da:Object):void{
				_baoGaoData=da;
				Laya.timer.loop(100,this,initData);
				ShiYanBaoGao.getInstance().event("getData");
				getSuccess=true;
			}
		}
		/**
		 *初始化数据 
		 * 
		 */		
		private function initData():void{
			//如果页面初始化完成，则初始化数据，如果页面初始化没有完成，则等待100毫秒后再检测
			if(!active){
				return;
			}
			Laya.timer.clear(this,initData);
			if(!_baoGaoData || _baoGaoData.length==0){
				return ;
			}
			if(myTeacher.length==0){
				myTeacher=_baoGaoData[0].itemvalue;
			}
			if(createUserName.length==0){
				createUserName=_baoGaoData[1].itemvalue;
			}
			if(syName.length==0){
				syName=_baoGaoData[2].itemvalue;
			}
			if(syMudi.length==0){
				syMudi=_baoGaoData[3].itemvalue;
			}
			if(syYuanLi.length==0){
				syYuanLi=_baoGaoData[4].itemvalue;
			}
			if(syDuixiang.length==0){
				syDuixiang=_baoGaoData[5].itemvalue;
			}
			if(syQiCai.length==0){
				syQiCai=_baoGaoData[6].itemvalue;
			}
			if(syYaoPin.length==0){
				syYaoPin=_baoGaoData[7].itemvalue;
			}
			if(syBuzhou.length==0){
				syBuzhou=_baoGaoData[8].itemvalue;
			}
			if(_baoGaoData[9]){
				syJieGuo=JSON.parse(_baoGaoData[9].itemvalue);
				trace("获取结果："+syJieGuo);
			}
			if(syTaoLun.length==0){
				syTaoLun=_baoGaoData[10].itemvalue;
			}
			if(syJieLun.length==0){
				syJieLun=_baoGaoData[11].itemvalue;
			}
		}
		/**
		 *提交实验报告 
		 * 
		 */		
		protected function submitHandler():void
		{
			// TODO Auto Generated method stub
			if(Data.isExam && Data.timeLimit){//如果是考核，则必须先完成实验，才能提交报告。
				if(!Data.isSyStepComplete){
					btn_pro.visible=true;
					btn_pro.text="请先完成实验";
					return;
				}
			}
			if(jiance()){
				var dlg:Dlg_XunWen=new Dlg_XunWen("一次实验只能提交一次实验报告，确认提交？","确认",Handler.create(this,sure),"取消");
				dlg.popup();
			}
			function sure():void{
				dlg.close();
				btn_submit.label="取消";
				btn_submit.off(Event.CLICK,this,submitHandler);
				btn_submit.once(Event.CLICK,this,cancleRequest);
				btn_pro.visible=true;
				btn_pro.text="正在提交……";
				ani_loading.visible=true;
				ani_loading.play();
				/*http=new HttpRequest();
				http.once(Event.COMPLETE,this,completeHandler);
				http.http.timeout = 3000;//设置超时时间；
				http.send(Data.HttpRequest+"Virtual/SaveMedicineExperiment",getBaogaodata(),"post","json");*/
				Api.SaveMedicineExperiment(Data.operationType,Data.PracticeId,getBaogaodata(),this,completeHandler)
			}
			
			/**取消请求**/
			function cancleRequest():void
			{
				// TODO Auto Generated method stub
				trace("取消");
				btn_submit.label="提交";
				btn_submit.on(Event.CLICK,this,submitHandler);
				btn_pro.visible=false;
				ani_loading.visible=false;
				ani_loading.stop();
			}
			/**
			 *提交成功 
			 * @param data
			 * 
			 */		
			function completeHandler(data:Object):void
			{
				btn_submit.off(Event.CLICK,this,cancleRequest);
				ani_loading.visible=false;
				ani_loading.stop();
				btn_submit.label="提交";
				btn_submit.on(Event.CLICK,this,submitHandler);
				if(data.success){
					btn_pro.text=data.msg;
					if(Data.isExam && Data.isNeedBaogao){//如果是考核并且考核内容包括实验报告，则考核完成
						examComplete();
					}
				}
				else{
					btn_pro.text="提交失败！"+data.msg;
					btn_pro.fontSize=16;
					btn_pro.color="#ffff00";
				}
			}
		}
		/**
		 *检测是否有空数据 
		 * @return 
		 * 
		 */		
		private function jiance():Boolean
		{
			// TODO Auto Generated method stub
			return true;
		}
		/**
		 *
		 * @return 实验报告提交的数据 ，可以重写 
		 * 
		 */		
		protected function getBaogaodata():Array
		{
			// TODO Auto Generated method stub
			var data:Array=[];
			data.push({itemname:'指导老师',itemvalue:myTeacher,itemsort:0})
			data.push({itemname:'实验人员',itemvalue:createUserName,itemsort:1})
			data.push({itemname:'实验名称',itemvalue:syName,itemsort:2})
			data.push({itemname:'实验目的',itemvalue:syMudi,itemsort:3})
			data.push({itemname:'实验原理',itemvalue:syYuanLi,itemsort:4})
			data.push({itemname:'实验对象',itemvalue:syDuixiang,itemsort:5})
			data.push({itemname:'实验器材',itemvalue:syQiCai,itemsort:6})
			data.push({itemname:'药品试剂',itemvalue:syYaoPin,itemsort:7})
			data.push({itemname:'实验步骤',itemvalue:syBuzhou,itemsort:8})
			data.push({itemname:'实验结果',itemvalue:JSON.stringify(syJieGuo),itemsort:9})
			data.push({itemname:'实验讨论',itemvalue:syTaoLun,itemsort:10})
			data.push({itemname:'实验结论',itemvalue:syJieLun,itemsort:11})
			return data;
		}
		/**
		 *考核模式 ：考核超时，未完成 
		 * 
		 */		
		private function timeOut():void
		{
			trace("实验报告：时间到");
			btn_submit.mouseEnabled=false;
			btn_submit.gray=true;
			btn_pro.visible=true;
			btn_pro.text="考核时间到，实验未完成！";
			getExamResult();
		}
		/**
		 *考核模式：考核完成
		 * 
		 */		
		public function examComplete():void
		{
			// TODO Auto Generated method stub
			//实验完成
			trace("考核完成");
			Data.isExamComplete=true;
			Api.SaveExperiment(Data.operationType,Data.PracticeId,Data.UsedTime);
			if(Browser.onPC){
				SceneToolControl.getInstance().pauseMusic();
			}
			else if(Browser.onMobile){
				SceneToolsWAP.getInstance().pauseMusic();
			}
			Laya.timer.clear(this,updateJieGuo);
			getExamResult();
		}
		/**
		 *考核模式：考核完成，提交考核结果 
		 * 
		 */		
		private function getExamResult():void
		{
			Laya.timer.clear(ShiYanBaoGao,updateJieGuo);
			Laya.timer.once(100,this,function():void{
				var resultDlg:PaiHangView=new PaiHangView();
				resultDlg.width=836;
				resultDlg.height=674;
				resultDlg.show();
			});
			
		}
		
		/**
		 *获取实验结果值 
		 * @return 
		 * 
		 */		
		public function get syJieGuo():Object
		{
			return _syJieGuo;
		}
		/**
		 *设定实验结果值 
		 * @param value
		 * 
		 */		
		public function set syJieGuo(value:Object):void
		{
			_syJieGuo = value;
		}
		public  function get createUserName():String
		{
			
			_createUserName=encodeURIComponent(ipt_name.text);
			return _createUserName;
		}
		public  function set createUserName(value:String):void
		{
			ipt_name.text=_createUserName = decodeURIComponent(value);
		}
		public  function get myTeacher():String
		{
			_myTeacher=encodeURIComponent(ipt_teacher.text);
			return _myTeacher;
		}
		
		public  function set myTeacher(value:String):void
		{
			ipt_teacher.text=_myTeacher = decodeURIComponent(value);
		}
		
		public  function get syName():String
		{
			_shiyanName=encodeURIComponent(ipt_syname.text);
			return _shiyanName;
		}
		
		public  function set syName(value:String):void
		{
			ipt_syname.text=_shiyanName = decodeURIComponent(value);
		}
		
		public  function get syMudi():String
		{
			_shiyanMudi=encodeURIComponent(ipt_symudi.text);
			return _shiyanMudi;
		}
		
		public  function set syMudi(value:String):void
		{
			ipt_symudi.text=_shiyanMudi = decodeURIComponent(value);
		}
		
		public  function get syYuanLi():String
		{
			_shiyanYuanLi=encodeURIComponent(ipt_syyuanli.text);
			return _shiyanYuanLi;
		}
		
		public  function set syYuanLi(value:String):void
		{
			ipt_syyuanli.text=_shiyanYuanLi = decodeURIComponent(value);
		}
		
		public  function get syDuixiang():String
		{
			_shiyanDuixiang=encodeURIComponent(ipt_duixiang.text);
			return _shiyanDuixiang;
		}
		
		public  function set syDuixiang(value:String):void
		{
			ipt_duixiang.text=_shiyanDuixiang = decodeURIComponent(value);
		}
		
		public  function get syQiCai():String
		{
			_shiyanQiCai=encodeURIComponent(ipt_qicai.text);
			return _shiyanQiCai;
		}
		
		public  function set syQiCai(value:String):void
		{
			ipt_qicai.text=_shiyanQiCai = decodeURIComponent(value);
		}
		
		public  function get syBuzhou():String
		{
			_shiyanBuzhou=encodeURIComponent(ipt_buzhou.text);
			return _shiyanBuzhou;
		}
		
		public  function set syBuzhou(value:String):void
		{
			ipt_buzhou.text=_shiyanBuzhou = decodeURIComponent(value);
		}
		public  function get syTaoLun():String
		{
			_shiyanTaoLun=encodeURIComponent(ipt_taolun.text);
			return _shiyanTaoLun;
		}
		
		public  function set syTaoLun(value:String):void
		{
			ipt_taolun.text=_shiyanTaoLun = decodeURIComponent(value);
		}
		
		public  function get syJieLun():String
		{
			_shiyanJieLun=encodeURIComponent(ipt_jielun.text);
			return _shiyanJieLun;
		}
		
		public  function set syJieLun(value:String):void
		{
			ipt_jielun.text=_shiyanJieLun = decodeURIComponent(value);
		}
		
		public  function get syYaoPin():String
		{
			_shiyanYaoPin=encodeURIComponent(ipt_shiji.text);
			return _shiyanYaoPin;
		}
		
		public  function set syYaoPin(value:String):void
		{
			ipt_shiji.text=_shiyanYaoPin = decodeURIComponent(value);
		}
	}
}